<?php
session_start();

if(isset($_SESSION['lang']))
    require_once "./language_files/mailingList_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/mailingList_en.php";

//retrieving user id by session
$member_id=$_SESSION['member_id'];
//Connect to the database

require_once "functions.php";

//check if the conference id  and the clicked letter are sent from the previouse page
if(isset($_POST['conference_id']) && isset($_POST['letter']))
{
    //retrieving conference id from previous page 
    $conference_id=intval($_POST['conference_id']);
    // check if the page recieve the letter sent to it from the letter link and save this letter in a variable
    $clicked_letter=$_POST['letter'];

    // query that gets the id, first and last names of member whose last names start with the letter clicked, in ascending order
    $members_list=mysql_query("Select distinct m.member_id, first_name, last_name 
            FROM member m, member_privileges 
            Where m.last_name LIKE '$clicked_letter%' 
            AND m.member_id != '$member_id' 
            AND ((m.member_id = member_privileges.member_id) 
            OR ( m.member_id  NOT IN (Select distinct m.member_id  
            FROM member_privileges,member m where m.member_id = member_privileges.member_id)) ) 
            ORDER BY last_name ASC") or die(mysql_error());


    //count the number of records of the previouse query
    $no_row=mysql_num_rows($members_list);
    // if there are no records resulted from the query, display a message
    if($no_row == 0)
        die($lang['error_1']);
    // a counter that its value is added to the check box and dropdown list for every member in the list to keep track of their ids to know later which of them checked to get the id of its member
    // loops on each member in the list by making every row in the selected table in an array
    while($line=mysql_fetch_assoc($members_list))
    {

        $first_name=formatText_safe($line['first_name']);
        $last_name=formatText_safe($line['last_name']);
        echo '<input type="checkbox" name="members[]" value="' . $line['member_id'] . '" >' . $first_name . ' ' . $last_name . '<br>';
    }
    // set the value of the previous page
}


// when the Done button is clicked after the member are chosen as well the committees they's assigned to,
// make sure the two arrays of the check boxes and drop down lists along with the conference id is sent from invitations page InvitationsDone method
if(isset($_POST['member_ids']) && isset($_POST['conference_id']) && isset($_POST['cfp']))
{
    //save the conference id in a variable
    $conference_id=intval($_POST['conference_id']);
    $cfpAnnouncement=$_POST['cfp'];
    // it loops on the check boxes to check which of them are checked
    for($j=0; $j < count($_POST['member_ids']); $j++)
    {
        //gets each member id which is the value of the check box
        $rec_id=$_POST['member_ids'][$j];
        // sanitizing the variable that will be inserted in the database
        $reciever_id=mysql_real_escape_string($rec_id);
        $mem=mysql_query("SELECT email FROM member WHERE member_id = '$reciever_id'");
        $memberRow=mysql_fetch_array($mem,MYSQL_ASSOC);
        $memEmail=$memberRow['email'];
        sendCFPemail($memEmail,$cfpAnnouncement,$conference_id);
    }
}
else
{
    echo '<script>',
    'alert("No members are checked");',
    '</script>';
}
// I will take them from the session that has been chosen in the checklist
function sendCFPemail($userEmail,$cfpAnn,$confid)
{

    //The conference long name is retrieved by calling the get_conference_name function from
    //functions.php and passing it the conference id of the conference for which the name
    //is needed, as a parameter.
    $confName=get_conference_name($confid);

    $subject=$confName + ' - Call for Papers';
    //a variable message is initialized containing the content of the email message.
    $message=<<<EMAIL
                        This is an international call for papers for the "'$confName'" Conference:\n
                        '$cfpAnn' \n
                        For more Information please click this link:
                        http://localhost:1234/ConfO/conferencePage.php?conference_id='$confid'}
EMAIL;
    //variable "from" specifies the sender
    $from='aya.elmobayad@gmail.com';
    //headers specifies the sender of the message
    $headers="From:" . $from;
    //a mail function is called to send the message
    mail($userEmail,$subject,$message,$headers);
}
echo'<button name="Done" onClick ="javascript:sendCallforPapers();">' . $lang['send'] . '</button>';
?>
            

